/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/phone/CallProgressSignalling.cpp,v 1.10 2003/11/21 16:12:58 itsme Exp $
 *
 *
 *  handles playing of various rings/tones/vibrates
 *
 */

#include "afxwin.h"
#include "CPhone.h"
#include "CPhoneDlg.h"
#include "SpipcHandler.h"
#ifdef _WIN32_WCE
#include "Vibrator.h"
#endif

#ifndef _WIN32_WCE
#include <mmsystem.h>
#endif

#include "CallProgressSignalling.h"

#include "PhoneOptions.h"

#include "debug.h"

#include <string>

// note: system wide only one sound may be active at any time, so beeps
// from other applications will interfere with our sound!

// this will initiate playing a sound in a loop, and immediately return.
static void PlayASyncSound(TCHAR *filename)
{
    std::wstring path(g_options.soundpath);
    path += L"\\";
    path += filename;
    ::PlaySound(path.c_str(), NULL, SND_ASYNC|SND_LOOP|SND_FILENAME);
}
// stop playing the sound started with PlayASyncSound.
static void StopSound()
{
    ::PlaySound(NULL, NULL, SND_ASYNC);
}


// this will start playing a sound, and wait for the sound to finish playing.
//   used to play the 'poing' - connect sound
static void PlaySound(TCHAR *filename)
{
    std::wstring path(g_options.soundpath);
    path += L"\\";
    path += filename;
    ::PlaySound(path.c_str(), NULL, SND_SYNC|SND_FILENAME);
}

CallProgressSignalling::CallProgressSignalling(CCPhoneDlg& dlg) :
    m_dlg(dlg)
{
    m_tone= CPTONE_NONE;
    m_bKeyexchange= false;
    m_bRinging= false;
}
CallProgressSignalling::~CallProgressSignalling()
{
}

void CallProgressSignalling::StartThread()
{
#ifdef _WIN32_WCE
    m_vibrator.StartThread();
#endif
/*
    NHThread::StartThread();
*/
}
void CallProgressSignalling::Start(TCallProgressTone tone)
{
    m_tone= tone;

    UpdateState();
}

void CallProgressSignalling::Stop()
{
    m_tone= CPTONE_NONE;

    UpdateState();
}

void CallProgressSignalling::StartKeyExchangeTone()
{
    m_bKeyexchange= true;

    UpdateState();
}

void CallProgressSignalling::StopKeyExchangeTone()
{
    m_bKeyexchange= false;

    UpdateState();
}

void CallProgressSignalling::StartRing()
{
#ifdef _WIN32_WCE
    m_vibrator.blink(1000, 3000);
#endif
    m_bRinging= true;

    UpdateState();
}


// generate comfort noise
void CallProgressSignalling::PlayComfortNoise()
{
    PlayASyncSound(L"completion.wav");
}

void CallProgressSignalling::SignalConnect()
{
    PlaySound(L"connect.wav");
}

void CallProgressSignalling::StopRing()
{
#ifdef _WIN32_WCE
    m_vibrator.off();
#endif
    m_bRinging= false;

    UpdateState();
}

void CallProgressSignalling::UpdateState()
{
    if (m_bRinging) {
        if (m_bKeyexchange)
        {
            debug("*************  RING RING + KEYEXCHANGE\n");
        }
        else
        {
            debug("*************  RING RING\n");
        }
        PlayASyncSound(L"ring.wav");
    }
    else if (m_tone==CPTONE_DIALING)
    {
        if (m_bKeyexchange)
        {
            debug("*************  WAKE WAKE + KEYEXCHANGE\n");
            PlayASyncSound(L"rrkx.wav");
        }
        else
        {
            debug("*************  WAKE WAKE\n");
            PlayASyncSound(L"rr.wav");
        }
    }
    else if (m_tone==CPTONE_ANSWERING)
    {
        debug("*************  ANSWERING\n");
        PlayASyncSound(L"kx.wav");
    }
    else if (m_tone==CPTONE_BUSY)   
    {
        debug("*************  BUSY\n");
        PlayASyncSound(L"busy.wav");
    }
    else if (m_bKeyexchange)
    {
        debug("*************  KEYEXCHANGE\n");
        PlayASyncSound(L"kx.wav");
    }
    else
    {
        debug("-------------  silence\n");
        StopSound();
    }
}

DWORD CallProgressSignalling::ThreadProc()
{
    // todo ---- change this into a class managing the ringtones
    // and playing them at regular intervals, just as the vibrator does.
    return 0;
}
